package com.adopapa.ethexplorer.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public final class RandomUtil {

//	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

	private static Random random = new Random();

	private static AtomicInteger atomicInteger = new AtomicInteger(0);

	public static String getRandom(long min, long max) {
		long tmp = Math.abs(random.nextLong());
		long result = tmp % (max - min + 1) + min;
		return String.valueOf(result);
	}

	public synchronized static int getIntNumber() {
		int result = atomicInteger.incrementAndGet();
		if (result == 65535) {
			atomicInteger.set(0);
		}
		return result;
	}

	/**
	 * 主键产生器
	 */
	public synchronized static String getLongTimeId() {
		Calendar calendar = Calendar.getInstance();
		try {
			Thread.sleep(1L);
			return String.format("%s%3d", dateFormat.format(calendar.getTime()), //
					calendar.get(Calendar.MILLISECOND));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 主键产生器
	 */
	public synchronized static String getTimeId() {
		Calendar calendar = Calendar.getInstance();
		try {
			Thread.sleep(1000L);
			return dateFormat.format(calendar.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public synchronized static String getUUIDStr() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	public synchronized static String getUUIDString() {
		return UUID.randomUUID().toString();
	}
	
	

	public static void main(String[] args) {
		System.out.println(getRandom(100000, 999999));
//		System.out.println(getIntNumber());
//		System.out.println(getIntNumber());
//		System.out.println(getIntNumber());
//		System.out.println(getIntNumber());
//		System.out.println(getIntNumber());
//		System.out.println(getIntNumber());
//		System.out.println(getIntNumber());
//		
//		System.out.println(getTimeId());
//		System.out.println(getTimeId());
//		System.out.println(getTimeId());
//		System.out.println(getTimeId());
//		
//		
//		System.out.println(MD5Utils.md5Encode("123456"));
//		System.out.println(MD5Utils.md5Encode("123456.."));
	}
}
